package com.gk.study.config;

import com.gk.study.interceptor.AccessInterceptor;
import com.gk.study.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@ComponentScan
public class MyConfig implements WebMvcConfigurer {
    @Autowired
    private LoginInterceptor loginInterceptor;
    //第三种处理跨域
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**")//项目中的所有接口都支持跨域
//                .allowedOriginPatterns("http://localhost:3000", "http://192.168.228.59:3000") //所有地址都可以访问，也可以配置具体地址
//                .allowCredentials(true)
//                .allowedMethods("*")//"GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS"
//                .allowedHeaders("*");
//    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //登录和注册不拦截
//       registry.addInterceptor(loginInterceptor).excludePathPatterns("/user/login","/user/register");
        registry.addInterceptor(loginInterceptor).addPathPatterns("/admin/**","/home/detail").excludePathPatterns("/user/login","/user/register","/guest/**");
        // 自定义拦截器
//        registry.addInterceptor(new AccessInterceptor());
    }

//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        // 自定义拦截器
//        registry.addInterceptor(new AccessInterceptor());
//    }
}


//进行 Spring MVC 的配置，包括注册拦截器和跨域设置。它使得你能够在应用中集中管理与 Web 相关的配置。
